package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"zk1/api/request"
	"zk1/internal/dao"
	"zk1/internal/model"
)

func AddSayReq(c *gin.Context) {
	var req request.AddSayReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	say := model.Says{

		UserId: int64(req.UserId),
		Test:   req.Test,
	}
	if !dao.CreateSay(&say) {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "添加失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "添加成功",
		"data": say,
	})
}
func UpdateSay(c *gin.Context) {
	var req request.UpdateSayReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	update := model.Says{
		UserId: 1,
		Test:   req.Test,
	}

	if !dao.Update(&update) {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "展示成功",
			"data": update,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "修改成功",
		"data": update,
	})
}

func LisxtSay(c *gin.Context) {
	var req request.ListSayReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	info := model.Test{Id: int64(req.UserId)}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "展示成功",
		"data": info,
	})
}

func RemoveSay(c *gin.Context) {
	var req request.RemoveSayReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	if !dao.Delete(&model.Test{Id: int64(req.UserId)}) {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "删除失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "删除成功",
		"data": req,
	})
}
